﻿using System;
using System.Data; 
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;


using Model;
using DAL;

public partial class login : System.Web.UI.Page
{
    private StudentManager studentManager;
    private TeacherManager teacherManager;
    private AdminManager adminManager;
    //private DBManagerFactory dbManagerFactory;
    

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string message = "";

        string userType = stylelist.SelectedItem.Value; //用户类型
        string userId = txtUsername.Text; //用户Id
        string password = txtPassword.Text; //密码

        string id;

        message += "用户名：" + txtUsername.Text + "<br />";
        message += "您的类型是：" + stylelist.SelectedItem.Text + "<br />";
        
        msg.Text = message;


          ///转向相应的页面
            switch (userType)
            {
                case "Student":
                    this.studentManager = new StudentManager();
                    Student student = new Student(userId,password);
                    id = studentManager.LoginReturnId(student);//登陆
                    if (id != null ) 
                    {                        
                        Session["userType"]=userType;   //  把用户类型和id保存到session中
                        Session["id"] = id;
                        Session["name"] = userId;
                        
                        Response.Redirect("Student/Default.aspx");
                    }
                    else
                    {
                        msg.Text += "登陆失败！";
                    }
                    //msg.Text += Session["userId"];
                    break;
                case "Teacher":
                    teacherManager = new TeacherManager();

                    Teacher teacher = new Teacher(userId, password);
                    id = teacherManager.LoginReturnId(teacher);
                    if (id != null)
                    {
                        Session["userType"] = userType;   //  把用户类型和id保存到session中
                        Session["id"] = id;
                        Session["name"] = userId;

                        Response.Redirect("Teacher/Default.aspx");
                    }
                    else
                    {
                        msg.Text += "登陆失败！";
                    }
                    




                    Response.Redirect("Teacher/Default.aspx");
                    break;
                case "Administrator":
                    Response.Redirect("Admin/Default.aspx");
                    break;
            }
        
    }
}
